package org.xmap.ui.control;

import javax.microedition.lcdui.Graphics;

import org.xmap.app.Env;
import org.xmap.ui.Theme;
import org.xmap.ui.UIUtil;
import org.xmap.ui.base.Event;
import org.xmap.ui.base.Window;

/**
 * <p>消息框控件，能容纳大概两行文本的提示框。</p>
 * @author Zealot
 * @version 1.0
 */
public final class MessageBox extends Window {

    /**
     * 第一行文字。
     */
    private String text;
    /**
     * 第一行文字的高亮部分。
     */
    private String textHightLight;
    /**
     * 第二行文字。
     */
    private String text2;
    /**
     * 第二行文字的高亮部分。
     */
    private String text2HightLight;
    /**
     * 消息框的文字的颜色。
     */
    private int textColor;
    /**
     * 消息框中高亮的文字的颜色。
     */
    private int hightLightColor;
    /**
     * 消息框的边框的颜色。
     */
    private int lineColor;

    /**
     * 默认构造函数。
     */
    public MessageBox() {
        super(0, 0, 0, 0, Window.ORIENT_LEFT_UP);
        this.textColor = 0xff000000;
        this.hightLightColor = 0xffff0000;
        this.lineColor = 0xff000000;
    }

    /**
     * 设置第一行文字消息。
     * @param text String 第一行文字。
     */
    public void setText(String text) {
        this.text = text;
        this.textHightLight = null;
    }

    /**
     * 设置第一行文字消息和高亮消息。
     * @param text String 文字消息。
     * @param textHightLight String 高亮消息。
     */
    public void setText(String text, String textHightLight) {
        this.text = text;
        this.textHightLight = textHightLight;
    }

    /**
     * 设置第二行文字消息。
     * @param text2 String 文字消息。
     */
    public void setText2(String text2) {
        this.text2 = text2;
        this.text2HightLight = null;
    }

    /**
     * 设置第二行文字消息和高亮消息。
     * @param text2 String 文字消息。
     * @param text2HightLight String 高亮消息。
     */
    public void setText2(String text2, String text2HightLight) {
        this.text2 = text2;
        this.text2HightLight = text2HightLight;
    }

    /**
     * 设置文字的颜色。
     * @param color int
     */
    public void setTextColor(int color) {
        this.textColor = color;
    }

    /**
     * 获得文字的颜色。
     * @return int 文字的颜色。
     */
    public int getTextColor() {
        return this.textColor;
    }

    /**
     * 设置高亮文字的颜色。
     * @param highLightColor int 高亮文字的颜色。
     */
    public void setHightLightColor(int highLightColor) {
        this.hightLightColor = highLightColor;
    }

    /**
     * 获得高亮文字的颜色。
     * @return int 高亮文字的颜色。
     */
    public int getHighLightColor() {
        return this.hightLightColor;
    }

    /**
     * 设置边框的颜色。
     * @param lineColor int 边框的颜色。
     */
    public void setLineColor(int lineColor) {
        this.lineColor = lineColor;
    }

    /**
     * 获得边框的颜色。
     * @return int 边框的颜色。
     */
    public int getLineColor() {
        return this.lineColor;
    }

    /**
     * 绘制函数。
     * @param g Graphics 图形句柄。
     */
    public void draw(Graphics g) {
        g.setFont(Env.instance.defaultFont);
        g.setColor(Theme.instance.submenuFocusedBackgroundColor);
        g.fillRect(Theme.instance.submenuCornerLU.getWidth(), Theme.instance.submenuTop.getHeight(), this.getRect().width - 2 * Theme.instance.submenuCornerLU.getWidth(), this.getRect().height - 2 * Theme.instance.submenuTop.getHeight());

        // 绘制中间部分
        if (Theme.instance.submenuCornerLU != null && Theme.instance.submenuLeft != null && Theme.instance.submenuTop != null) {
            int step = Theme.instance.submenuLeft.getHeight();
            for (int i = Theme.instance.submenuCornerLU.getHeight(); i + step <= this.getRect().height - Theme.instance.submenuCornerLU.getHeight(); i += step) {
                // 左侧
                g.drawImage(Theme.instance.submenuLeft, 0, i, Graphics.LEFT | Graphics.TOP);
                // 右侧
                g.drawImage(Theme.instance.submenuRight, this.getRect().width - Theme.instance.submenuLeft.getWidth(),
                        i, Graphics.LEFT | Graphics.TOP);
            }
            g.drawImage(Theme.instance.submenuLeft, 0,
                    this.getRect().height - Theme.instance.submenuCornerLU.getHeight() - step,
                    Graphics.LEFT | Graphics.TOP);
            g.drawImage(Theme.instance.submenuRight,
                    this.getRect().width - Theme.instance.submenuCornerLU.getWidth(),
                    this.getRect().height - Theme.instance.submenuCornerLU.getHeight() - step,
                    Graphics.LEFT | Graphics.TOP);
//            // 上面中部
            step = Theme.instance.submenuTop.getWidth();
            for (int i = Theme.instance.submenuCornerLU.getWidth(); i + step <= this.getRect().width; i += step) { // 顶部
                g.drawImage(Theme.instance.submenuTop, i,
                        0, Graphics.LEFT | Graphics.TOP);
                g.drawImage(Theme.instance.submenuBottom, i,
                        this.getRect().height - Theme.instance.submenuCornerLU.getHeight(), Graphics.LEFT | Graphics.TOP);
            }
            g.drawImage(Theme.instance.submenuTop, this.getRect().width - step - Theme.instance.submenuCornerLU.getWidth(),
                    0, Graphics.LEFT | Graphics.TOP);
            g.drawImage(Theme.instance.submenuBottom, this.getRect().width - step - Theme.instance.submenuCornerLU.getWidth(),
                    this.getRect().height - Theme.instance.submenuCornerLU.getHeight(), Graphics.LEFT | Graphics.TOP);

            g.drawImage(Theme.instance.submenuCornerLU, 0, 0, // 左上
                    Graphics.LEFT | Graphics.TOP);
            g.drawImage(Theme.instance.submenuCornerRU, this.getRect().width - Theme.instance.submenuCornerLU.getWidth(), // 右上
                    0, Graphics.LEFT | Graphics.TOP);
            g.drawImage(Theme.instance.submenuCornerLD, 0, // 左下
                    this.getRect().height - Theme.instance.submenuCornerLU.getHeight(), Graphics.LEFT | Graphics.TOP);
            g.drawImage(Theme.instance.submenuCornerRD, this.getRect().width - Theme.instance.submenuCornerLU.getWidth(), // 右下
                    this.getRect().height - Theme.instance.submenuCornerLU.getHeight(), Graphics.LEFT | Graphics.TOP);
        }




        if (this.bgImage != null) {
            if (super.getBackgroundImageStretch() || super.getBackgroundImageCenter()) {
                g.drawImage(this.bgImage, this.getRect().width >> 1, this.getRect().height >> 1, Graphics.VCENTER | Graphics.HCENTER);
            } else {
                for (int i = 0; i < this.getRect().width; i += this.bgImage.getWidth()) {
                    for (int j = 0; j < this.getRect().height; j += this.bgImage.getHeight()) {
                        g.drawImage(this.bgImage, i, j, Graphics.LEFT | Graphics.TOP);
                    }
                }
            }
        }
        String text = null;
        String textH = null;
        boolean firstRow = false;
        if (this.text != null || this.textHightLight != null) {
            firstRow = true;
            text = this.text;
            textH = this.textHightLight;
        }
        boolean secondRow = false;
        if (this.text2 != null || this.text2HightLight != null) {
            secondRow = true;
            text = this.text2;
            textH = this.text2HightLight;
        }
        if (firstRow && secondRow) { // 两行同时存在
            int base = this.getRect().height >> 2;
            if (this.textHightLight != null && this.text != null) {
                int xCenter = this.getRect().width >> 1;
                int text2Width = Env.instance.defaultFont.stringWidth(this.text + this.textHightLight) >> 1;
                g.setColor(this.textColor);
                UIUtil.draw(g, this.text, xCenter - text2Width, base, Graphics.LEFT, Graphics.VCENTER);
                g.setColor(this.hightLightColor);
                UIUtil.draw(g, this.textHightLight, xCenter + text2Width, base, Graphics.RIGHT, Graphics.VCENTER);
            } else {
                if (this.text != null) {
                    g.setColor(this.textColor);
                    UIUtil.draw(g, this.text, this.getRect().width >> 1, base, Graphics.HCENTER, Graphics.VCENTER);
                } else if (this.textHightLight != null) {
                    g.setColor(this.hightLightColor);
                    UIUtil.draw(g, this.textHightLight, this.getRect().width >> 1, base, Graphics.HCENTER, Graphics.VCENTER);
                }
            }
            base += this.getRect().height >> 1;
            if (this.text2HightLight != null && this.text2 != null) {
                int xCenter = this.getRect().width >> 1;
                int text2Width = Env.instance.defaultFont.stringWidth(this.text2 + this.text2HightLight) >> 1;
                g.setColor(this.textColor);
                UIUtil.draw(g, this.text2, xCenter - text2Width, base, Graphics.LEFT, Graphics.VCENTER);
                g.setColor(this.hightLightColor);
                UIUtil.draw(g, this.text2HightLight, xCenter + text2Width, base, Graphics.RIGHT, Graphics.VCENTER);
            } else {
                if (this.text2 != null) {
                    g.setColor(this.textColor);
                    UIUtil.draw(g, this.text2, this.getRect().width >> 1, base, Graphics.HCENTER, Graphics.VCENTER);
                } else if (this.text2HightLight != null) {
                    g.setColor(this.hightLightColor);
                    UIUtil.draw(g, this.text2HightLight, this.getRect().width >> 1, base, Graphics.HCENTER, Graphics.VCENTER);
                }
            }
        } else { // 如果一行，居中显示
            int base = this.getRect().height >> 1;
            if (text != null && textH != null) {
                int xCenter = this.getRect().width >> 1;
                int text2Width = Env.instance.defaultFont.stringWidth(text + textH) >> 1;
                g.setColor(this.textColor);
                UIUtil.draw(g, text, xCenter - text2Width, base, Graphics.LEFT, Graphics.VCENTER);
                g.setColor(this.hightLightColor);
                UIUtil.draw(g, textH, xCenter + text2Width, base, Graphics.RIGHT, Graphics.VCENTER);
            } else {
                if (text != null) {
                    g.setColor(Theme.instance.submenuFocusedTextColor);//提示文字改这里
                    UIUtil.draw(g, text, this.getRect().width >> 1, base, Graphics.HCENTER, Graphics.VCENTER);
                } else if (textH != null) {
                    g.setColor(this.hightLightColor);
                    UIUtil.draw(g, textH, this.getRect().width >> 1, base, Graphics.HCENTER, Graphics.VCENTER);
                }
            }
        }
        if (super.getShowRect()) {
            g.setColor(this.lineColor);
            g.drawRect(0, 0, this.getRect().width - 1, this.getRect().height - 1);
        }
    }

    /**
     * MessageBox控件不处理任何按键消息
     * @param flag boolean 是按下还是抬起。
     * @param keyCode int 按键代码。
     * @param gameAction int 动作代码。
     * @return boolean 是否处理该事件。
     */
    public boolean keyEvent(boolean flag, int keyCode, int gameAction) {
        return false;
    }

    /**
     * 触笔事件处理函数，在MessageBox区域内的触笔事件都被处理。
     * @param ev Event 触笔事件。
     * @return boolean 是否处理该事件。
     */
    public boolean pointerEvent(Event ev) {
        if (this.getRect().collideWith(ev.param1, ev.param2)) {
            return true;
        }
        return false;
    }
}
